

function PromiseAll(arr){
    return new Promise((resolve,reject)=>{
        if(arr.length == 0){
            resolve([])
        }
        let index = 0;
        let result=[]
        arr.forEach(item=>{
            Promise.resolve(item).then(res=>{
                result[index] = res
                index++
                if(index == arr.length){
                    resolve(result)
                }
            }).catch((err)=>{
                reject(err)
            })
        })
    })
}

let p1 = new Promise((resolve,reject)=>{
    resolve('1')
})
let p2 = new Promise((resolve,reject)=>{
    resolve(2)
})
let p3 = new Promise((resolve,reject)=>{
    reject(4)
})
PromiseAll([p1,p2,p3]).then(res=>{
    console.log(res)
}).catch(err=>{
    console.log(err)
})